package com.cssw.fyzb.tps.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cssw.fyzb.tps.model.vo.AddressVo;
import com.cssw.fyzb.tps.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Feng Chen
 */
@Component
public class LbsClient {

    @Value("${lbs.url}")
    private String url;

    @Value("${lbs.secret}")
    private String secret;

    private static final Logger logger = LoggerFactory.getLogger(LbsClient.class);

    /**
     * 经纬度转地址
     *
     * @param lat 纬度
     * @param lng 经度
     * @return
     */
    public AddressVo getAddress(String lat, String lng) {
        Map<String, Object> param = new HashMap<>();
        param.put("location", String.format("%s,%s", lat, lng));
        param.put("get_poi", "0");
        param.put("key", secret);
        try {
            String str = HttpUtil.get(url, param);
            logger.info("location :  {}", str);
            JSONObject data = JSON.parseObject(str);
            JSONObject result = data.getJSONObject("result");
            return result.getObject("address_component", AddressVo.class);
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }


}
